package com.lineage.server.clientpackets;

import com.lineage.config.ConfigAlt;
import com.lineage.config.ConfigRate;
import com.lineage.config.ConfigWho;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.pr_type_name;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.S_WhoCharinfo;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.server.ServerRestartTimer;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldNpc;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Who extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Who.class);
  
  public static int deCount() {
    int count = 0;
    Collection<L1NpcInstance> allObj = WorldNpc.get().all();
    if (allObj.isEmpty())
      return count; 
    Iterator<L1NpcInstance> iterator = allObj.iterator();
    while (iterator.hasNext()) {
      L1NpcInstance obj = iterator.next();
      if (obj instanceof L1DeInstance)
        count++; 
    } 
    return count;
  }
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      L1DeInstance l1DeInstance = null;
      read(decrypt);
      String s = readS();
      L1PcInstance l1PcInstance1 = World.get().getPlayer(s);
      L1PcInstance pc = client.getActiveChar();
      if (l1PcInstance1 == null)
        l1DeInstance = getDe(s); 
      if (l1DeInstance != null) {
        S_WhoCharinfo whoChar = new S_WhoCharinfo((L1Character)l1DeInstance, client);
        pc.sendPackets((ServerBasePacket)whoChar);
      } else {
        int de = deCount();
        int count = World.get().getAllPlayers().size();
        String amount = String.valueOf((int)(count * ConfigAlt.ALT_WHO_COUNT) + de);
        if (pc.isGm()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\f=(GM显示)目前真实人数: " + amount));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\f=(GM显示)目前假人人数: " + de));
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\f=目前人数: " + amount));
        if (ConfigAlt.ALT_WHO_COMMANDX) {
          String info[], nowDate = (new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")).format(new Date());
          switch (ConfigAlt.ALT_WHO_TYPE) {
            case 0:
              pc.sendPackets(
                  (ServerBasePacket)new S_ServerMessage("\\fV启动时间: " + String.valueOf(ServerRestartTimer.get_startTime())));
              if (ConfigWho.check_who_exp) {
                pc.sendPackets(
                    (ServerBasePacket)new S_ServerMessage("\\fV经验倍率: " + (ConfigWho.whoexp * ConfigWho.RATE_XP_WHO)));
              } else {
                pc.sendPackets(
                    (ServerBasePacket)new S_ServerMessage("\\fV经验倍率: " + (ConfigRate.RATE_XP * ConfigWho.RATE_XP_WHO)));
              } 
              pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV金钱倍率: " + ConfigRate.RATE_DROP_ADENA));
              if (ConfigWho.check_who_weapon) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV冲武倍率: " + ConfigWho.whoweapon));
              } else {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV冲武倍率: " + ConfigRate.ENCHANT_CHANCE_WEAPON));
              } 
              if (ConfigWho.check_who_armor) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV冲防倍率: " + ConfigWho.whoarmor));
              } else {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV冲防倍率: " + ConfigRate.ENCHANT_CHANCE_ARMOR));
              } 
              pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV现实时间: " + nowDate));
              pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fV重启时间: " + ServerRestartTimer.get_restartTime()));
              if (pc.hasSkillEffect(7596)) {
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("请稍候在进行操作。"));
                return;
              } 
              pc.setSkillEffect(7596, 60000);
              if (pc.getnewaicount_2() > 0)
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU您的验证答案数字请输入: " + pc.getnewaicount_2())); 
              break;
            case 1:
              info = new String[14];
              info[1] = String.valueOf(amount);
              if (ConfigWho.check_who_exp) {
                info[2] = String.valueOf(ConfigWho.whoexp * ConfigWho.RATE_XP_WHO);
              } else {
                info[2] = String.valueOf(ConfigRate.RATE_XP * ConfigWho.RATE_XP_WHO);
              } 
              info[3] = String.valueOf(ConfigRate.RATE_DROP_ITEMS);
              info[4] = String.valueOf(ConfigRate.RATE_DROP_ADENA);
              if (ConfigWho.check_who_weapon) {
                info[5] = String.valueOf(ConfigWho.whoweapon);
              } else {
                info[5] = String.valueOf(ConfigRate.ENCHANT_CHANCE_WEAPON);
              } 
              if (ConfigWho.check_who_armor) {
                info[6] = String.valueOf(ConfigWho.whoarmor);
              } else {
                info[6] = String.valueOf(ConfigRate.ENCHANT_CHANCE_ARMOR);
              } 
              info[7] = String.valueOf(ConfigAlt.POWER);
              info[8] = String.valueOf(ConfigAlt.POWERMEDICINE);
              info[9] = String.valueOf(ConfigAlt.MEDICINE);
              info[10] = String.valueOf(nowDate);
              info[11] = String.valueOf(ServerRestartTimer.get_restartTime());
              info[12] = String.valueOf(pc.getElixirStats());
              info[13] = String.valueOf(pc.getguaji_count());
              pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "y_who", info));
              if (pc.getPrestigeLv() != 0)
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("目前" + pr_type_name._1 + ":" + pc.getPrestige(), 11)); 
              if (pc.hasSkillEffect(7596)) {
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("请稍候在进行操作。"));
                return;
              } 
              pc.setSkillEffect(7596, 60000);
              if (pc.getnewaicount_2() > 0)
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU您的验证答案数字请输入: " + pc.getnewaicount_2())); 
              if (pc.getguaji_count() > 0)
                pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU目前活力值: " + pc.getguaji_count())); 
              break;
          } 
        } 
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private L1DeInstance getDe(String s) {
    Collection<L1NpcInstance> allObj = WorldNpc.get().all();
    if (allObj.isEmpty())
      return null; 
    Iterator<L1NpcInstance> iterator = allObj.iterator();
    while (iterator.hasNext()) {
      L1NpcInstance obj = iterator.next();
      if (obj instanceof L1DeInstance) {
        L1DeInstance de = (L1DeInstance)obj;
        if (de.getNameId().equalsIgnoreCase(s))
          return de; 
      } 
    } 
    return null;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
